//
//  RecorderView.m
//  MooreDoll
//
//  Created by Orange on 2014/12/29.
//  Copyright (c) 2014年 Tank. All rights reserved.
//
#define AVAudioSessionShare [AVAudioSession sharedInstance]

#import "RecorderView.h"
#import <AVFoundation/AVFoundation.h>

@interface RecorderView()

@property (weak, nonatomic) IBOutlet UILabel    *labelTime;
@property (weak, nonatomic) IBOutlet UIButton   *buttonSaveRecord;
@property (weak, nonatomic) IBOutlet UIButton   *buttonAgainRecord;
@property (weak, nonatomic) IBOutlet UIButton   *buttonRecorderAction;

@property (nonatomic) BOOL                       recordFinish;
@property (nonatomic) BOOL                       audioRecording;
@property (nonatomic) BOOL                       audioPlaying;

@end

@implementation RecorderView

- (void)drawRect:(CGRect)rect {
    _recordFinish   = NO;
    if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewDrawRect)]) {
        [_delegate recorderViewDrawRect];
    }
}
#pragma mark - IBAction
- (IBAction)saveRecorderPress:(id)sender {
    if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewSaveRecorder)]) {
        [_delegate recorderViewSaveRecorder];
    }
}
- (IBAction)againRecrderPress:(id)sender {
    [_labelTime setHidden: YES];
    [_labelTime setText: @"00 : 00"];
    [self buttonHidden: YES];
    [self buttonAction: _buttonRecorderAction inName: @"audioStop"];
    
    _recordFinish   = NO;
    if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewAgainRecrder)]) {
        [_delegate recorderViewAgainRecrder];
    }
}
- (IBAction)recorderAction:(UIButton *)sender {
    if (!_recordFinish)
        [self audioRecorderButton: sender];
    else
        [self audioPlayerButton: sender];
}
#pragma mark - 其他
- (void) audioRecorderButton:(UIButton *) sender {
    if (!_audioRecording) {
        [self buttonAction: sender inName: @"audioStart"];
        [_labelTime setHidden: NO];
        if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewAudioStart)]) {
            [_delegate recorderViewAudioStart];
        }

    }
    else {
        [self buttonAction: sender inName: @"audioPlay"];
        [self buttonHidden: NO];
        _recordFinish = YES;
        if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewAudioPlay)]) {
            [_delegate recorderViewAudioPlay];
        }

    }
}
- (void) audioPlayerButton:(UIButton *) sender {
    if (!_audioPlaying) {
        [self buttonAction: sender inName: @"audioPause"];
        [_labelTime setText: @"00 : 00"];

        if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewAudioPause)]) {
            [_delegate recorderViewAudioPause];
        }
    }
    else {
        [self buttonAction: sender inName: @"audioPlay"];
        if ( _delegate && [_delegate respondsToSelector: @selector(recorderViewAudioStop)]) {
            [_delegate recorderViewAudioStop];
        }
    }
}

#pragma mark - Button
- (void) buttonHidden: (BOOL) hidden {
    [_buttonSaveRecord  setHidden: hidden];
    [_buttonAgainRecord setHidden: hidden];
}
-(void) buttonAction:(UIButton *) sender inName:(NSString *) imageName {
    [sender setImage: [UIImage imageNamed: imageName]  forState: UIControlStateNormal];
}
#pragma mark - Set 
- (void) audioRecording: (BOOL) audioBool {
    NSLog(audioBool ? @"audioRecording YES" : @"audioRecording NO");
    _audioRecording = audioBool;
}
- (void) audioPlaying: (BOOL) audioBool {
    NSLog(audioBool ? @"audioPlaying YES" : @"audioPlaying NO");

    _audioPlaying = audioBool;
}
- (void) labelTimeText: (NSString *) text {
    [_labelTime setText: text];
}
- (void)playFinish {
    [self buttonAction: _buttonRecorderAction inName: @"audioPlay"];
    _audioPlaying = NO;
}
@end
